package com.example.movie.ui.cinema.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import butterknife.ButterKnife
import com.example.movie.R

class ConstellationAdapter(
    private val context: Context,
    private val list: List<String>
) : BaseAdapter() {
    private var checkItemPosition = 0
    fun setCheckItem(position: Int) {
        checkItemPosition = position
        notifyDataSetChanged()
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun getItem(position: Int): Any? {
        return null
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(
        position: Int,
        convertView: View?,
        parent: ViewGroup
    ): View? {
        var convertView = convertView
        val viewHolder: ViewHolder
        if (convertView != null) {
            viewHolder =
                convertView.tag as ViewHolder
        } else {
            convertView =
                LayoutInflater.from(context).inflate(R.layout.item_constellation, null)
            viewHolder =
                ViewHolder(
                    convertView
                )
            convertView.tag = viewHolder
        }
        fillValue(position, viewHolder)
        return convertView
    }

    private fun fillValue(
        position: Int,
        viewHolder: ViewHolder
    ) {
        viewHolder.mText!!.text = list[position]
        if (checkItemPosition != -1) {
            if (checkItemPosition == position) {
                viewHolder.mText!!.setTextColor(
                    context.resources.getColor(R.color.drop_down_selected)
                )
                viewHolder.mText!!.setBackgroundResource(R.drawable.bg_check)
            } else {
                viewHolder.mText!!.setTextColor(
                    context.resources.getColor(R.color.drop_down_unselected)
                )
                viewHolder.mText!!.setBackgroundResource(R.drawable.bg_uncheck)
            }
        }
    }

    internal class ViewHolder(view: View?) {
        val mText: TextView = view!!.findViewById(R.id.text)

        init {
            ButterKnife.inject(this, view)
        }
    }

}